var express = require('express');
const ObjectId = require('mongodb').ObjectId;

var router = express.Router();

var Word = require('../models/word')
var Classe = require('../models/classe')




/*
根据用户id查询所有单词，按类别划分
db.classes.aggregate([{
     $lookup:
          {
            from: "words",
            localField: "_id",
            foreignField: "cid",
            as: "wlist"
          }
    }])
*/
//uid=5e6e54c4d146361ef4848421
router.get('/getc/:uid', function (req, res, next) {
    var uid = ObjectId(req.params.uid);
    Classe.aggregate([{
        $lookup:
        {
            from: "words",
            localField: "_id",
            foreignField: "cid",
            as: "wlist"
        }
    }, {
        // 查询条件，对前面的查询结果再次过滤
        $match: {
            uid: uid
        }
    }], function (err, doc) {
        if (err) {
            res.json({
                status: '1',
                msg: err.message
            })
        } else {
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});

//查询某个类别下所有单词
//cid=ObjectId("5e70af30360c6555b369fb8a")
router.get('/get/:cid', function (req, res, next) {
    //获取id
    var cid = ObjectId(req.params.cid);
    Word.find({ cid: cid }, function (err, doc) {
        if (err) {
            res.json({
                status: '1',
                msg: err.message
            })
        } else {
            res.json({
                status: '0',
                msg: '',
                result: {
                    count: doc.length,
                    list: doc
                }
            })
        }
    })
});

//saveWords为二级路由，测试数据添加
//ObjectId("5e732628a9653c54de24345a")
router.get('/save', function (req, res, next) {
    var cid = ObjectId("5e732628a9653c54de24345a");



    var myobj = [
        { name: 'explain', cid: cid, explain: '解释' },
        { name: 'beast', cid: cid, explain: '野兽' },
        { name: 'aggregate', cid: cid, explain: '聚合' }

    ];

    Word.insertMany(myobj, function (err, result) {
        // if (err) throw err;
        if (err) {
            res.json({
                status: '1',
                msg: err.message
            })
        } else {
            // res.json({
            //     status: '0',
            //     msg: '',
            //     result: {
            //         count: result.insertedCount,
            //         list: result
            //     }
            // })

            Classe.update({ _id: cid }, {$inc:{wordsNumber:myobj.length}}, function (err1, doc) {
                if (err1) {
                    res.json({
                        status: '1',
                        msg: err1.message
                    })
                }else {
                    res.json({
                        status: '0',
                        msg: '',
                        result: {
                            count: result.insertedCount,
                            list: result
                        }
                    })
                    // console.log(doc);
                    // console.log('update!');
                }
            })
        }

    });


});





module.exports = router;